import urllib.request
import ssl

# 小坑不知道为什么一直请求不成功， 百度了下加上这个之后就好了
ssl._create_default_https_context = ssl._create_unverified_context
url = 'https://www.baidu.com/'

# 带了https的地址拥有反爬机制，我们可以携带 ua 来模拟浏览器

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
}

request = urllib.request.Request(url=url, headers=headers)
print(type(request))

response = urllib.request.urlopen(request)
#
content = response.read().decode('utf-8')
print(content)
